﻿var bigfootWeb = {

    log: function (msg) {
        try { console.log(msg); } catch (e) { }
    },

    errorHappened: false,

    connectAjax: function () {
        // Create html element
        jQuery("<div id='bigfootWeb_flash'></div>").appendTo("body");

        // Clear the flash
        bigfootWeb.clearGlobalMessage();

        // Get the flash element
        var flash = jQuery("#bigfootWeb_flash");

        // Set a request header to flag every ajax request
        flash.ajaxSend(function (ev, req, options) {
            req.setRequestHeader("ajaxRequest", "true");
        });

        // Mark the page as updating on every ajax call 
        flash.ajaxStart(function () {
            bigfootWeb.showGlobalLoading("Updating");
        });

        // ERROR HANDLER. Determine if the error is due to security and redirect to the login page.
        flash.ajaxError(function (event, request, settings) {
            bigfootWeb.log("AJAX ERROR: " + request.status + " | " + request.responseText);
            bigfootWeb.errorHappened = true;
            if (request.status == 402) {
                window.location = loginurl;
            }
            bigfootWeb.showGlobalError(request.status + ': ' + request.responseText); // request.getResponseHeader("ActionErrorMessage")); //showGlobalError(request.status + ': ' + request.statusText);
        });

        // Hide the Loading message on successful completion
        flash.ajaxSuccess(function (request, settings) {
            bigfootWeb.clearGlobalMessage();
        });
    },

    clearGlobalMessage: function () {
        if (jQuery("#bigfootWeb_flash") != null)
            return jQuery("#bigfootWeb_flash").removeClass("flash_notice flash_alert flash_loading").html("").hide();
    },

    showGlobalError: function (msg) {
        var f = bigfootWeb.clearGlobalMessage();
        f.addClass("flash_alert");
        bigfootWeb.showGlobalMessage(f, msg);
    },

    showGlobalNotice: function (msg) {
        var f = bigfootWeb.clearGlobalMessage();
        f.addClass('flash_notice');
        //f.addClass("flash_notice").html(msg).show();
        bigfootWeb.showGlobalMessage(f, msg);
    },

    showGlobalLoading: function (msg) {
        var f = bigfootWeb.clearGlobalMessage();
        f.addClass("flash_loading");
        bigfootWeb.showGlobalMessage(f, msg);
    },

    showGlobalMessage: function (jobj, msg) {
        jobj.animate({ top: jQuery(window).scrollTop() + "px" }, { queue: false, duration: 350 });
        jobj.append(msg).show();
        var w = jobj.width();
    },

    removePostField: function () {
        for (var i = 0; i < formData.length; i++) {
            if (formData[i].name == fieldName) {
                formData.splice(i, 1);
            }
        }
        return formData;
    },

    getFormData: function () {
        var formData = jQuery("form:eq(0)").formToArray();

        // Remove ASP.NET hidden fields
        bigfootWeb.removePostField(formData, '__VIEWSTATE');
        bigfootWeb.removePostField(formData, '__EVENTTARGET');
        bigfootWeb.removePostField(formData, '__EVENTARGUMENT');

        return formData;
    }
}
